﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Facade.Manager.Abstract;

using Domain.Model;

using DataAccess.Repositories;

namespace Facade.Manager
{
    public class FormManager : AbstractManager
    {
        #region Members

        private static readonly FormManager instance = new FormManager();

        // FormRepository
        private FormRepository formRepository;

        #endregion

        #region Properties

        public List<Form> Forms { get; private set; }

        #endregion

        #region FormManager

        private FormManager()
        {
            // instantiate FormRepository
            formRepository = new FormRepository();

            // get forms
            Forms = formRepository.GetAll() as List<Form>;

            // create forms, if they don´t exist
            if (Forms == null || Forms.Count == 0)
            {
                CreateForms();
                Forms = formRepository.GetAll() as List<Form>;
            }
        }

        public static FormManager Instance
        {
            get 
            {
                return instance;
            }
        }

        #endregion

        #region public methods

        /// <summary>
        /// Get a specific form by it´s Id property
        /// </summary>
        /// <param name="id"></param>
        /// <returns>A form</returns>
        public Form GetFormById(int id)
        {
            Form match = null;

            foreach (var form in Forms)
            {
                if (form.Id == id)
                {
                    match = form;
                }
            }

            return match;
        }

        /// <summary>
        /// Get the form for projects
        /// </summary>
        /// <returns>A form</returns>
        public Form GetProjectForm()
        {
            Form projectForm = null;

            foreach (var form in Forms)
            {
                if (form.Name.Equals(Form.PROJECT_FORM))
                {
                    projectForm = form;
                }
            }

            return projectForm;
        }

        /// <summary>
        /// Get the form for subprojects
        /// </summary>
        /// <returns>A form</returns>
        public Form GetSubprojectForm()
        {
            Form subprojectForm = null;

            foreach (var form in Forms)
            {
                if (form.Name.Equals(Form.SUBPROJECT_FORM))
                {
                    subprojectForm = form;
                }
            }

            return subprojectForm;
        }

        /// <summary>
        /// Creates a new form element and adds it to the
        /// related form
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        public void AddFormElement(int formId, string name, string type)
        {
            var form = GetFormById(formId);

            if (form != null)
            {
                int position = form.FormElements.Count;
                var element = new FormElement(name, position, type);

                form.AddFormElement(element);

                UpdateForm(form);
            }
            else
            { 
                // do something
            }
        }

        /// <summary>
        /// Deletes the form element with the given name from
        /// the form with the given id
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="name"></param>
        public void DeleteFormElementByName(int formId, string name)
        {
            var form = GetFormById(formId);

            if (form != null)
            {
                var formElement = form.GetElementByName(name);

                if (formElement != null)
                {
                    formRepository.DeleteFormElement(formElement);
                    form.RemoveFormElement(formElement);

                    UpdateForm(form);

                    if (form.Name.Equals(Form.PROJECT_FORM))
                    {
                        ProjectManager.Instance.DeleteDetailFromProjects(name);
                    }

                    if (form.Name.Equals(Form.SUBPROJECT_FORM))
                    {
                        SubprojectManager.Instance.DeleteDetailFromSubprojects(name);
                    }
                }
            }
            else
            {
                // do something
            }
        }

        #endregion

        #region private methods

        /// <summary>
        /// Updates a form in the database
        /// </summary>
        /// <param name="form"></param>
        private void UpdateForm(Form form)
        {
            formRepository.Update(form);
        }

        private void CreateForms()
        {
            Form projectForm = new Form(Form.PROJECT_FORM);
            Form subprojectForm = new Form(Form.SUBPROJECT_FORM);

            formRepository.Add(projectForm);
            formRepository.Add(subprojectForm);
        }

        #endregion
    }
}